﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Security.Cryptography;
using System.Text;
namespace NJL.Web.AdminVisitor
{
    public partial class PersonalInformation : BasePage
    {
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        Model.NJL_USER modelUser;
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.BLL.NJL_CS_LIKES bllLikes = new BLL.NJL_CS_LIKES();
        NJL.Model.NJL_PERSON modelPerson;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session["Token"])
            {
                SetToken();
            }
            if (!IsPostBack)
            {
                bind();
            }
        }
        public string GetToken()
        {
            if (null != Session["Token"])
            {
                return Session["Token"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        private void SetToken()
        {
            Session.Add("Token", UserMd5(Session.SessionID + DateTime.Now.Ticks.ToString()));
        }
        protected string UserMd5(string str1)
        {
            string cl1 = str1;
            string pwd = "";
            MD5 md5 = MD5.Create();
            // 加密后是一个字节类型的数组 
            byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得 
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
        }
        void bind()
        {
            modelUser = bllUser.GetModel(UserId);
            txtUserName.Text = modelUser.USER_NAME;  //用户名
            txtTel.Text = modelUser.TEL;             //电话
            txtMobel.Text = modelUser.MOBILE;
            txtEmail.Text = modelUser.EMAIL;
            txtQQ.Text = modelUser.QQ;
            txtMSN.Text = modelUser.MSN;
            DataSet ds = bllLikes.GetList("");
            cbxlLove.DataSource = ds;
            cbxlLove.DataTextField = "AREA_NAME";
            cbxlLove.DataValueField = "AREA_CODE";
            cbxlLove.DataBind();
            ddlType.Items.Add(new ListItem("身份证", "身份证"));
            ddlType.Items.Add(new ListItem("军官证", "军官证"));
            ddlType.Items.Add(new ListItem("护照", "护照"));

            modelPerson = bllPerson.GetModelByUserId(UserId);
            txtName.Text = modelPerson.REAL_NAME;
            //if (modelPerson.SEX == "男")
            //{
            //    rblSex.SelectedIndex = 0;
            //}
            //else
            //{
            //    rblSex.SelectedIndex = 1;
            //}
            ddlType.Items.FindByValue(modelPerson.ID_TYPE);
            txtNumber.Text = modelPerson.ID_CODE;
            txtBirthday.Text = string.Format("{0:yyyy-MM-dd}", modelPerson.BIRTHDAY);
            txtEducation.Text = modelPerson.DEGREE;
            //string[] str = modelPerson.LIKES.Split(',');
            //foreach (string id in str)
            //{
            //    cbxlLove.Items.FindByValue(id).Selected = true;
            //}
            //ddlType.Items.FindByValue(modelPerson.ID_TYPE).Selected = true;
            if (!string.IsNullOrEmpty(modelPerson.IMAGE_CODE))
            {
                Image1.ImageUrl = GetImageUrl(modelPerson.IMAGE_CODE);
            }
            else
            {
                Image1.Visible = false;
            }
        }
        protected void btnSumbit_Click(object sender, EventArgs e)
        {
            modelUser = bllUser.GetModel(UserId);
            modelUser.USER_NAME = txtUserName.Text.Trim();
            modelUser.TEL = txtTel.Text.Trim();
            modelUser.MOBILE = txtMobel.Text.Trim();
            modelUser.EMAIL = txtEmail.Text.Trim();
            modelUser.QQ = txtQQ.Text.Trim();
            modelUser.MSN = txtMSN.Text.Trim();
            if (bllUser.Update(modelUser))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "PersonalInformation.aspx");
                Page.ClientScript.RegisterStartupScript(GetType(), "style", "<script>infotab('baseinfo','baseinfocss')</script>");
            }
        }
        protected void btnVisitor_Click(object sender, EventArgs e)
        {
            modelPerson = bllPerson.GetModelByUserId(UserId);
            modelPerson.REAL_NAME = txtName.Text.Trim();
            modelPerson.SEX = rblSex.SelectedValue;
            modelPerson.ID_TYPE = ddlType.SelectedValue;
            modelPerson.DEGREE = txtEducation.Text.Trim();
            if (txtBirthday.Text.Trim() != "")
            {
                modelPerson.BIRTHDAY = Convert.ToDateTime(txtBirthday.Text.Trim());
            }
            modelPerson.ID_CODE = txtNumber.Text.Trim();
            modelPerson.UPDATE_DATE_TIME = DateTime.Now;
            string likes = string.Empty;
            foreach (ListItem li in cbxlLove.Items)
            {
                if (li.Selected == true)
                {
                    likes += li.Value + ",";
                }
            }
            if (!string.IsNullOrEmpty(likes))
            {
                modelPerson.LIKES = likes.Substring(0, likes.Length - 1);
            }
            if (bllPerson.Update(modelPerson))
            {
                Royalsoft.Common.MessageBox.Show(this, "更新成功");
                Page.ClientScript.RegisterStartupScript(GetType(), "style1", "<script>infotab('visitorinfo','visitorinfocss')</script>");
            }
        }
        protected void btnHeadImg_Click(object sender, EventArgs e)
        {
            if (!Request.Form.Get("hiddenTestN").Equals(GetToken()))
            {   
                return;
            }
            SetToken();
            modelPerson = bllPerson.GetModelByUserId(UserId);
            if (FileUpload1.HasFile)
            {
                if (!string.IsNullOrEmpty(modelPerson.IMAGE_CODE))
                {
                    DeleteImageByCode(modelPerson.IMAGE_CODE);
                }
                if (modelPerson.IMAGE_CODE == "")
                {
                    string message;
                    if (HandleIntegral(UserId, "1", out message))
                    {

                    }
                }
                modelPerson.IMAGE_CODE = SaveUploadFile(Request.Files);
                if (bllPerson.Update(modelPerson))
                {
               
                    if (!string.IsNullOrEmpty(modelPerson.IMAGE_CODE))
                    {
                        Image1.ImageUrl = GetImageUrl(modelPerson.IMAGE_CODE);
                        Image1.Visible = true;

                     
                        Royalsoft.Common.MessageBox.Show(this, "上传成功");
           

                    }
                    else
                    {
                        Image1.Visible = false;
                    }
                }
            }
            Page.ClientScript.RegisterStartupScript(GetType(), "style1", "<script lanage='javascript'>infotab('head','headcss')</script>");
        }
    


    }
}
